#include "http/http.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>



int main(int c,char **v) {
	struct robot_context *context=http_robot_context_build();

	struct sockaddr_in addr;
	http_addr_reset(&addr,"61.135.169.125",80);
	
	struct buffer *request;
	http_request_head(context,&request,M_GET,"/","html/text","61.135.169.125", 80, CONN_TYPE_CLOSE,NULL);
	
	struct buffer *response;
	http_fetch(context,request,&addr,&response);
	
	printf("%s \n\n%d",response->data,response->index);
	
	int fd=open("baidu.html",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
	
	int wr_len=response->index;
	int offset=0;
	int ret;
	while(wr_len){
		ret=write(fd,(response->data)+offset,wr_len);
		offset +=ret;
		wr_len -=ret;
	}
	close(fd);
	buffer_del(context->factory,request);
	buffer_del(context->factory,response);
	
	http_robot_context_release(context);
	return 0;
}
